﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Kooboo.Commerce
{
    class Calculator
    {
        public static void Calculate(ICart cart)
        {
            cart.Discount = 0;
            cart.InsuredAmount = 0;
            cart.PaymentMethodCost = 0;
            cart.ShippingCost = 0;
            cart.SubTotal = 0;
            cart.Total = 0;
            cart.TotalDiscount = 0;
            cart.TotalTaxCost = 0;

            var totalWeight = 0m;

            var discountContext = new PromotionContext();
            discountContext.CouponCode = cart.Coupon; //add by zhenquan
            var taxContext = new TaxContext()
                {
                    Country = cart.Country,
                    State = cart.State,
                };


            //total
            foreach (var item in cart.CartItems)
            {
                item.SubTotal = 0;
                item.Discount = 0;

                item.UnitPrice = item.SaleItem.RetailPrice;

                item.SubTotal = item.UnitPrice * item.Quantity;
                item.Total = item.SubTotal;

                cart.SubTotal += item.SubTotal;

                cart.Total += item.Total;

                totalWeight = item.Product.Weight.HasValue ? item.Product.Weight.Value * item.Quantity : 0;

                discountContext.Items.Add(new PromotionItem()
                {
                    Discount = 0,
                    IsDiscounted = false,
                    Product = item.Product,
                    Quantity = item.Quantity,
                    SubTotal = item.SubTotal,
                    RelatedObject = item
                });

                taxContext.Items.Add(new TaxItem
                {
                    Product = item.Product,
                    RelatedObject = item
                });
            }

            //discount    
            discountContext.Total = cart.Total;
            discountContext.Execute();
            cart.Total = discountContext.Total;
            cart.Discount = discountContext.Discount;

            foreach (var item in discountContext.Items)
            {
                cart.TotalDiscount += item.Discount;
            }
            cart.TotalDiscount += cart.Discount;

            //tax
            taxContext.Execute();

            foreach (var item in taxContext.Items)
            {
                var cartItem = item.RelatedObject as ICartItem;
                cartItem.TaxCost = cartItem.Total * item.TaxPercent / 100;
                cart.TotalTaxCost += cartItem.TaxCost;

                cartItem.Total += cartItem.TaxCost;
            }

            //shippingcost
            cart.ShippingCost = cart.Shipping.RateCost(totalWeight);

            //payment
            if (cart.Payment != null && cart.Payment.AmountChange.HasValue && cart.Payment.AmountChange.Value > 0)
            {
                cart.PaymentMethodCost = cart.Payment.AmountChange.Value;
            }
            else if (cart.Payment != null && cart.Payment.PercentChange.HasValue && cart.Payment.PercentChange.Value > 0)
            {
                cart.PaymentMethodCost = cart.Total * cart.Payment.PercentChange.Value / 100;
            }
            else
            {
                cart.PaymentMethodCost = 0;
            }

            //tax,shiping,payment allowances
            cart.TotalTaxCost = discountContext.TaxAllowance > cart.TotalTaxCost ? 0 : cart.TotalTaxCost - discountContext.TaxAllowance;
            cart.ShippingCost = discountContext.ShippingAllowance > cart.ShippingCost ? 0 : cart.ShippingCost - discountContext.ShippingAllowance;
            cart.PaymentMethodCost = discountContext.PaymentAllowance > cart.PaymentMethodCost ? 0 : cart.PaymentMethodCost - discountContext.PaymentAllowance;

            cart.Total += cart.PaymentMethodCost;
            cart.Total += cart.TotalTaxCost;
            cart.Total += cart.ShippingCost;
        }
    }
}
